<?php
namespace app\home\controller;

use think\Controller;
use think\Db;
use app\home\model\Mechanism as MechanismModel;
use app\home\model\InstitutionalActivities as InstitutionalActivitiesModel;
use app\api\model\User  as  UserModel;
use EasyWeChat\Foundation\Application;
use think\Config;
use org\Qiniu;
use app\api\model\Banner as  BannerModel;
class First extends Common  {

    /***
     * @return mixed
     * 首页
     */
    public function index(){
       session('target_url',"http://south.xuanwumobile.com/home/First/index");
       $openid = $this->getOpenid();
       $this->getUserId($openid);
       $user_id =  session('u_openid'); 
        //我的未读消息
//        $user_id = 75;
        $uid = ','.$user_id.',';
        $where1['n_user_id'] = array('like',"%$uid%");
        $where1['n_read_user'] = array('notlike',"%$uid%");
        $where1['n_clear_user'] = array('notlike',"%$uid%");
        $count  = Db::table('up_notice')->where($where1)->count();
        if($count > 99){
            $count = '99+';
        }
        //Banner图
        $field = 'banner_img,banner_id,banner_link,banner_title,activities_id,classroom_id,mechanism_id,infomation_id,detail_type';
        $res =Db::table('up_banner')->where('banner_status',1)->field($field)->order('banner_time desc')->limit(6)->select();
        //活动分类
        $activity_type = Db::table('up_activity')->where('id','NEQ',1)->select();

        //志愿者之星
        $list = Db::table('up_volunteer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_mechanism um','um.mechanism_id=uuu.user_to_mechanism_id')
            ->field('a.*,uuu.user_username,uuu.user_image,um.mechanism_intro')
            ->limit(3)
            ->order('a.volunteer_sort desc')
            ->select();
        //热门问答
        $now_time = date('Y-m-d H:i:s',time());
        $last_time = date('Y-m-d H:i:s',strtotime('-1 week'));
        $where['interlocution_time'] =array('between',array($last_time,$now_time));
        $answer = Db::table('up_interlocution')->where($where)->order('interlocution_num desc')->limit(5)->select();
        foreach($answer as $key=>&$vo){
            $res_s = Db::table('up_interlocution_answer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->where('f_interlocution_id',$vo['interlocution_id'])
                ->field('user_username,user_image')
                ->order('answer_time desc')
                ->find();
            $vo['user_username'] =$res_s['user_username'];
            $vo['user_image'] =$res_s['user_image'];
        }
        //热门活动
        $now_time1   = date("Y-m-d H:i:s", time());
        $after_time1 = date("Y-m-d H:i:s", strtotime("+1 week"));
        $fi = 'a.activity_status,a.activity_address,a.id,a.activity_name,a.activity_img,a.activity_start_time,a.activity_end_time,a.activity_id';
        $where2['activity_status'] = 2;
        $where2['activity_start_time'] = array('between',array($now_time1,$after_time1));
        $where2['activity_sh'] = 1;
        $where2['is_show'] = 1;
        $resList = Db::table('up_institutional_activities')->alias('a')
            ->where($where2)
            ->order('activity_num desc')
            ->field($fi)
            ->select();
        foreach ($resList as $kk=>&$vv){

            $activity_name =Db::table('up_activity')->where('id','IN',trim($vv['activity_id'],','))->column('activity_name');
            $vv['activity_names'] = implode(',',$activity_name);
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }

        }
        $data =array(
            'num'=>$count,
            'img'=>$res,
            'list'=>$list,
            'answer'=>$answer,
            'hot_activity'=>$resList,
       );
        $this->assign('user_id',$user_id);
        $this->assign('activity_type',array_chunk($activity_type,4));
        $this->assign('data',$data);
        return $this->fetch();
    }

    /****
     * banner详情
     */
    public function bannerdetail(){
        session('target_url',"http://south.xuanwumobile.com/home/First/bannerdetail");
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $banner_id = input('param.banner_id');
        $BannerModel = new BannerModel();
        $field='banner_id,banner_img,banner_title,banner_content';
        $res = $BannerModel->selectBannerOne($field,$banner_id);
        $this->assign('res',$res);
        return $this->fetch();
    }
    /**
     * @return mixed
     * 搜索活动历史
     */
    public function search(){
        session('target_url',"http://south.xuanwumobile.com/home/First/search");
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $type = input('param.type')?input('param.type'):1;
        $user_id =  session('u_openid');
        $where['search_user_id'] = $user_id;
        $where['search_type'] = $type;
        $list = Db::table('up_user_search')->where($where)->order('search_datetime desc')->limit(5)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    /**
     * @return mixed
     * 搜索活动历史-活动列表页面
     */
    public function searchAct(){
        session('target_url',"http://south.xuanwumobile.com/home/First/searchAct");
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $type = input('param.type')?input('param.type'):1;
        $user_id =  session('u_openid');
        $where['search_user_id'] = $user_id;
        $where['search_type'] = $type;
        $list = Db::table('up_user_search')->where($where)->order('search_datetime desc')->limit(5)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /***
     * @return mixed
     * 搜索机构历史
     */
    public function sactivity(){
        session('target_url',"http://south.xuanwumobile.com/home/First/sactivity");
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $type = input('param.type')?input('param.type'):2;
        $user_id =  session('u_openid');
        $where['search_user_id'] = $user_id;
        $where['search_type'] = $type;
        $list = Db::table('up_user_search')->where($where)->order('search_datetime desc')->limit(5)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 搜索结果  - 机构
     */
    public function mechanism(){
        $user_id =  session('u_openid');
        $MechanismModel = new MechanismModel();
        $type = input('param.type')?input('param.type'):2;//1.活动   2.机构
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $list = $MechanismModel->getMechanismList($keyword,$page,2);
        $whereMap['search_user_id'] = $user_id;
        $whereMap['search_content'] = $keyword;
        $whereMap['search_type'] = 2;
        $res = Db::table('up_user_search')->where($whereMap)->value('search_id');
        if(!$res){//没有查到就插入用户搜索记录表
            $data['search_content'] = $keyword;
            $data['search_type'] = $type;
            $data['search_user_id'] = $user_id;
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->insert($data);
        }else{
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->where('search_id',$res)->update($data);
        }
        $this->assign('list',$list);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 机构ajax请求
     */
    public function ajaxmechanism(){
        $user_id = session('u_openid');
        $MechanismModel = new MechanismModel();
        $type = input('param.type');//1.活动   2.机构
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $list = $MechanismModel->getMechanismList($keyword,$page,2);
        $whereMap['search_user_id'] = $user_id;
        $whereMap['search_content'] = $keyword;
        $whereMap['search_type'] = 2;
        $res = Db::table('up_user_search')->where($whereMap)->value('search_id');
        if(!$res){//没有查到就插入用户搜索记录表
            $data['search_content'] = $keyword;
            $data['search_type'] = $type;
            $data['search_user_id'] = $user_id;
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->insert($data);
        }
        return json(['status'=>200,'msg'=>$list]);
    }

    /**
     * @return mixed
     * 搜索活动结果
     */
    public function searchactivity(){
        $user_id = session('u_openid');
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $type = input('param.type')?input('param.type'):1;//1.活动   2.机构
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $list = $InstitutionalActivitiesModel->getActivitys('','*',$page,2,$keyword,20);
        $whereMap['search_user_id'] = $user_id;
        $whereMap['search_content'] = $keyword;
        $whereMap['search_type'] = 1;
        $res = Db::table('up_user_search')->where($whereMap)->value('search_id');
        if(!$res){//没有查到就插入用户搜索记录表
            $data['search_content'] = $keyword;
            $data['search_type'] = $type;
            $data['search_user_id'] = $user_id;
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->insert($data);
        }else{
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->where('search_id',$res)->update($data);
        }
        foreach ($list as $key=>&$vv){
            $activity_name =Db::table('up_activity')->where('id','IN',trim($vv['activity_id'],','))->column('activity_name');
            $vv['activity_names'] = implode(',',$activity_name);
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        $this->assign('list',$list);
        $this->assign('keyword',$keyword);

        return $this->fetch();
    }
    
    /**
     * @return mixed
     * 搜索活动结果
     */
    public function searchActList(){
        $user_id = session('u_openid');
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $type = input('param.type')?input('param.type'):1;//1.活动   2.机构
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $list = $InstitutionalActivitiesModel->getActivitys('','*',$page,2,$keyword,20);
        $whereMap['search_user_id'] = $user_id;
        $whereMap['search_content'] = $keyword;
        $whereMap['search_type'] = 1;
        $res = Db::table('up_user_search')->where($whereMap)->value('search_id');
        if(!$res){//没有查到就插入用户搜索记录表
            $data['search_content'] = $keyword;
            $data['search_type'] = $type;
            $data['search_user_id'] = $user_id;
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->insert($data);
        }else{
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->where('search_id',$res)->update($data);
        }
        foreach ($list as $key=>&$vv){
            $activity_name =Db::table('up_activity')->where('id','IN',trim($vv['activity_id'],','))->column('activity_name');
            $vv['activity_names'] = implode(',',$activity_name);
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        $this->assign('list',$list);
        $this->assign('keyword',$keyword);
        
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * ajax请求搜索活动
     */
    public function ajaxactivity(){
        $user_id = session('u_openid');
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $type = input('param.type')?input('param.type'):1;//1.活动   2.机构
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $list = $InstitutionalActivitiesModel->getActivitys('','*',$page,2,$keyword,5);
        $whereMap['search_user_id'] = $user_id;
        $whereMap['search_content'] = $keyword;
        $whereMap['search_type'] = 1;
        $res = Db::table('up_user_search')->where($whereMap)->value('search_id');
        if(!$res){//没有查到就插入用户搜索记录表
            $data['search_content'] = $keyword;
            $data['search_type'] = $type;
            $data['search_user_id'] = $user_id;
            $data['search_datetime'] = date('Y-m-d H:i:s',time());
            Db::table('up_user_search')->insert($data);
        }
        foreach ($list as $key=>&$vv){
            $activity_name =Db::table('up_activity')->where('id','IN',trim($vv['activity_id'],','))->column('activity_name');
            $vv['activity_names'] = implode(',',$activity_name);
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        return json(['status'=>200,'msg'=>$list]);

    }
    /***
     * 删除用户搜索历史记录
     */
    public function clear(){
        $type = input('param.type');
        if($type==1){//删除单个
            $where['search_id'] = input('param.search_id');
            $list = Db::table('up_user_search')->where($where)->delete();
            if($list){
                $this->apiSuccess();
            }else{
                $this->apiError('E001');
            }
        }elseif ($type==2){//清楚所有
            $where['search_user_id'] = session('u_openid');
            $where['search_type'] = input('param.type_lx');
            $count = Db::table('up_user_search')->where($where)->count();
            if($count>0){
                $list = Db::table('up_user_search')->where($where)->delete();
                if($list){
                    $this->apiSuccess();
                }else{
                    $this->apiError('E001');
                }
            }else{
                $this->apiError(array('msg' => "没有数据不能删除", 'status' => 'L200'));
            }
        }

    }
    /***
     * @return mixed
     * 活动
     */
    public function activity(){
        $id= input('param.id');
        $page = input('param.page')?input('param.page'):1;
        session('target_url',"http://south.xuanwumobile.com/home/First/activity?id=".$id.'&page='.$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id =  session('u_openid');


        $res = Db::table('up_activity')->select();

        $now_time   = date("Y-m-d H:i:s", time());

        Db::table('up_institutional_activities')
            ->where('activity_start_time','>',$now_time)
            ->setField('is_gq',2);
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $after_time = date("Y-m-d H:i:s", strtotime("+1 week"));
        $where['activity_sh'] = 1;
        $where['is_show'] = 1;
        $field = 'a.activity_status,a.activity_address,a.id,a.activity_name,a.activity_img,a.activity_start_time,a.activity_end_time,a.activity_id';
        if($id==1){//热门
            $where['activity_status'] = 2;
            
            $where['activity_start_time'] = array('between',array($now_time,$after_time));
            $list = $InstitutionalActivitiesModel->selectList($where,'desc',$page,$field);
        }else{//后台配置的
            $ids = ','.$id.',';
            $where['activity_id'] = array('like',"%$ids%");
            $where['activity_status'] = array('IN','1,2,3,5');
            $list = $InstitutionalActivitiesModel->selectList($where,'asc',$page,$field,2);
        }
        foreach ($list as $key=>&$vv){
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        $this->assign('res',$res);
        $this->assign('type_id',$id);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * ajax请求活动
     */
    public function ajaxactivitylist(){
        $now_time   = date("Y-m-d H:i:s", time());
        $page = input('param.page')?input('param.page'):1;

        $id = input('param.id');
        Db::table('up_institutional_activities')
            ->where('activity_start_time','>',$now_time)
            ->setField('is_gq',2);
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $after_time = date("Y-m-d H:i:s", strtotime("+1 week"));
        $where['activity_sh'] = 1;
        $where['is_show'] = 1;
        $field = 'a.activity_status,a.activity_address,a.id,a.activity_name,a.activity_img,a.activity_start_time,a.activity_end_time,a.activity_id';
        if($id==1){//热门
            $where['activity_status'] = 2;
            $where['activity_start_time'] = array('between',array($now_time,$after_time));
            $list = $InstitutionalActivitiesModel->selectList($where,'desc',$page,$field);
        }else{//后台配置的
            $ids = ','.$id.',';
            $where['activity_id'] = array('like',"%$ids%");
            $where['activity_status'] = array('IN','1,2,3');
            $list = $InstitutionalActivitiesModel->selectList($where,'asc',$page,$field,2);
        }
        foreach ($list as $key=>&$vv){
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        return json(['status'=>200,'msg'=>$list]);

    }
    /***
     * @return mixed
     * 活动详情
     */
    public function activitydatail(){
        $page = input('param.page')?input('param.page'):1;
        $id = input('param.activity_id');//活动ID
        session('target_url',"http://south.xuanwumobile.com/home/First/activitydatail?activity_id=".$id.'&page='.$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $user_id =  session('u_openid');
        $where['user_id'] = $user_id;
        $where1['a.id'] = $id;
        //这条活动的信息
        $list = Db::table('up_institutional_activities')->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.activity_mechanism_id','left')
            ->where($where1)->field('a.*,um.mechanism_name')->find();
        $activity_name =Db::table('up_activity')->where('id','IN',trim($list['activity_id'],','))->column('activity_name');
        $list['name'] = implode(',',$activity_name);
        $list['activity_start_times'] = $list['activity_start_time'];
        $where['institutional_activities_id'] = $id;
        $my_activities = Db::table('up_my_activities')->where($where)->value('id');//查询该用户该活动是否已经收藏
        //查询该用户该活动是否已经参加这个活动
        $my_activities_member = Db::table('up_institutional_activities_member')->where($where)->find();
        $now_time = time();//报名的现在时间
        if(strtotime($list['activity_sign_start'])>0&&strtotime($list['activity_sign_end'])>0){
            //设置活动报名的时间
            if(strtotime($list['activity_sign_start'])<$now_time && $now_time < strtotime($list['activity_sign_end'])-7200){
//                if($my_activities_member['sign_status']==1){
//                    $whereMap['user_id'] = $user_id;
//                    $whereMap['institutional_activities_id'] = $id;
//                    Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
//                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
//                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                }else{
//                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
//                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                }
                $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                $list['already_sign'] = $my_activities_member2['sign_status'];
            }else{
                if(strtotime($list['activity_sign_end'])<$now_time&&
                    $now_time<strtotime($list['activity_start_time'])-7200){
                    $list['already_sign'] = 5;//活动的报名结束时间到活动的开始时间的空白期
                }else{
                    if(strtotime($list['activity_start_time'])-7200<$now_time&&$now_time<strtotime($list['activity_end_time'])){
                        if($my_activities_member['sign_status'] ==1){
                            $whereMap['user_id'] = $user_id;
                            $whereMap['institutional_activities_id'] = $id;
                            Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                        }

                        $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                        $list['already_sign'] = $my_activities_member2['sign_status'];
                    }else{
                        $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                        $list['already_sign'] = $my_activities_member2['sign_status'];
                    }

                }
            }
//                if(strtotime($list['activity_sign_end'])<$now_time&&
//                    $now_time<strtotime($list['activity_start_time'])-7200){
//                    $list['already_sign'] = 5;//活动的报名结束时间到活动的开始时间的空白期
//                }else{
//                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
//                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                }

        }else{//没有设置活动报名时间
            if(strtotime($list['create_time'])<$now_time&&$now_time <strtotime($list['activity_end_time'])){
                if($my_activities_member['sign_status']==1){
                    $whereMap['user_id'] = $user_id;
                    $whereMap['institutional_activities_id'] = $id;
                    Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                    $list['already_sign'] = $my_activities_member2['sign_status'];
                }else{
                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                    $list['already_sign'] = $my_activities_member['sign_status'];
                }
            }else{
                $list['already_sign'] = $my_activities_member['sign_status'];
            }
        }
        if($my_activities_member['id']==''){//该用户该活动的么有报名状态
            $list['already_sign'] = 4;
            $list['sign_time_member'] ='';
            $list['sign_end_time_member'] ='';
        }else{
            $list['sign_time_member'] =$my_activities_member['sign_time'];
            $list['sign_end_time_member'] =$my_activities_member['sign_end_time'];
        }
        if($list['activity_status']==3){//进行中查询该活动的评论
            $num = $page-1;//分页
            $res = Db::table('up_institutional_activities_comment')//评论
            ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
                ->where('activities_id',$list['id'])
                ->field('a.*,uuu.user_username,uuu.user_image')
                ->limit($num*10,10)
                ->select();
            $count = Db::table('up_institutional_activities_comment')//评论
            ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
                ->where('activities_id',$list['id'])
                ->count();
            $list['count'] = $count;
            foreach ($res as $vk=>&$vv){//评论点赞
                $start_time = date('Ymd',strtotime($vv['answer_time']));
                if($start_time==date('Ymd',time())){//同年同月同日
                    $vv['answer_time'] = date('m-d',strtotime($vv['answer_time'])).'   '.date('H:i',strtotime($vv['answer_time']));
                }elseif (date('Y',strtotime($vv['answer_time']))!=date('Y',time())){
                    //不同年
                    $vv['answer_time'] = date('Y-m-d',strtotime($vv['answer_time'])).'  '.date('H:i',strtotime($vv['answer_time']));
                }else{
                    $vv['answer_time'] = date('m-d',strtotime($vv['answer_time'])).'  '.date('H:i',strtotime($vv['answer_time']));
                }
                $where2['data_id'] = $vv['answer_id'];
                $where2['type'] = 1;
                $where2['user_id'] = $user_id;
                $dz_id = Db::table('up_dz')->where($where2)->value('dz_id');
                if($dz_id){//证明点赞过了
                    $vv['dz'] = 1;
                }else{//没有点赞
                    $vv['dz'] = 2;
                }
                if($user_id == $vv['answer_user_id']){//证明是自己发的
                    $vv['zj_fb'] = 1; //评论自己发布的
                }else{
                    $vv['zj_fb'] = 2; //不是自己发布
                }
                $count_answer = Db::table('up_institutional_activities_answer')
                    ->alias('a')
                    ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                    ->where('a.reply_information_answer_id',$vv['answer_id'])
                    ->count();
                $vv['count_answer'] = $count_answer;
                $aa = explode(',',trim($vv['answer_img'],','));
                if(count(array_filter($aa))>0){
                    $vv['answer_img'] = $aa;
                }else{
                    $vv['answer_img'] = array();
                }
            }
            foreach ($res as $key=>$vo){//对评论进行回复
                $a= Db::table('up_institutional_activities_answer')
                    ->alias('a')
                    ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                    ->where('a.reply_information_answer_id',$vo['answer_id'])
                    ->limit(2)
                    ->field('a.*,uuu.user_username')
                    ->order('a.reply_create_time desc')
                    ->select();
                $res[$key]['answer'] = $a;
            }
            $list['first_answer'] = $res;//回复评论
        }
        if($my_activities){
            $list['collection'] = 1;//证明已经收藏了
        }else{
            $list['collection'] = 2;//证明没有收藏了
        }
        $start_time = date('Ymd',strtotime($list['activity_start_time']));
        if($start_time==date('Ymd',time())){//同年同月同日
            $list['activity_start_time'] = date('m月d日',strtotime($list['activity_start_time'])).'   '.date('H:i',strtotime($list['activity_start_time']));
            $list['activity_end_time'] =   date('H:i',strtotime($list['activity_end_time']));
        }elseif (date('Y',strtotime($list['activity_start_time']))!=date('Y',time())){
            //不同年
            $list['activity_start_time'] = date('Y年m月d日',strtotime($list['activity_start_time'])).'  '.date('H:i',strtotime($list['activity_start_time']));
            $list['activity_end_time'] =   date('H:i',strtotime($list['activity_end_time']));
        }else{
            $list['activity_start_time'] = date('m月d日',strtotime($list['activity_start_time'])).'  '.date('H:i',strtotime($list['activity_start_time']));
            $list['activity_end_time'] =   date('m月d日 H:i',strtotime($list['activity_end_time']));
        }
        $list['time'] = time();
        $this->assign('list',$list);
        $this->assign('user_id',$user_id);
        $this->assign('id',$id);
        return $this->fetch();
    }
    /***
     * ajax评论加载更多
     */
    public function ajaxactivitypl(){
        $user_id = session('u_openid');
        $page = input('param.page')?input('param.page'):1;
        $id = input('param.activity_id');//活动ID
        $num = $page-1;//分页
        $res = Db::table('up_institutional_activities_comment')//评论
        ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
            ->where('activities_id',$id)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->limit($num*10,10)
            ->select();
        foreach ($res as $vk=>&$vv){//评论点赞
            $start_time = date('Ymd',strtotime($vv['answer_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['answer_time'] = date('m-d',strtotime($vv['answer_time'])).'   '.date('H:i',strtotime($vv['answer_time']));
            }elseif (date('Y',strtotime($vv['answer_time']))!=date('Y',time())){
                //不同年
                $vv['answer_time'] = date('Y-m-d',strtotime($vv['answer_time'])).'  '.date('H:i',strtotime($vv['answer_time']));
            }else{
                $vv['answer_time'] = date('m-d',strtotime($vv['answer_time'])).'  '.date('H:i',strtotime($vv['answer_time']));
            }
            $aa = explode(',',$vv['answer_img']);

            if(count(array_filter($aa))>0){
                $vv['img'] =$aa;
            }else{
                $vv['img'] =array();
            }
            $where2['data_id'] = $vv['answer_id'];
            $where2['type'] = 1;
            $where2['user_id'] = 1;
            $dz_id = Db::table('up_dz')->where($where2)->value('dz_id');
            if($dz_id){//证明点赞过了
                $vv['dz'] = 1;
            }else{//没有点赞
                $vv['dz'] = 2;
            }
            if($user_id == $vv['answer_user_id']){//证明是自己发的
                $vv['zj_fb'] = 1; //评论自己发布的
            }else{
                $vv['zj_fb'] = 2; //不是自己发布
            }
            $a= Db::table('up_institutional_activities_answer')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                ->where('a.reply_information_answer_id',$vv['answer_id'])
                ->limit(2)
                ->field('a.*,uuu.user_username')
                ->order('a.reply_create_time desc')
                ->select();
            $count= Db::table('up_institutional_activities_answer')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
                ->where('a.reply_information_answer_id',$vv['answer_id'])
                ->count();
            $vv['count'] = $count;
            foreach ($a as $kk=>&$vo){
                if($vo['reply_user_id'] == $user_id){
                    $vo['is_delete'] = 1;//可以删除
                }else{
                    $vo['is_delete'] = 2;//不可以删除
                }
            }
            $res[$vk]['answer'] = $a;
        }

        return json(['status'=>200,'msg'=>$res]);

    }
    /**
     * 活动评论页面
     */
    public function activitylist(){
        $id = input('param.id');

        session('target_url',"http://south.xuanwumobile.com/home/First/activitylist?id=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id =  session('u_openid');
        $options = Config::get('wechat');
        $app = new Application($options);
        $js = $app->js;
        $json = $js->config(array('getLocation','chooseImage','uploadImage','downloadImage','getLocalImgData'), false ,false,true);
        $this->assign(['json'=>$json,'uid'=>input('uid')]);
        $this->assign('id',$id);
        return $this->fetch();
    }
    /**
     * 活动评论
     */
    public function activityadd()
    {
        $id = input('param.id');//活动ID

        session('target_url',"http://south.xuanwumobile.com/home/First/activityadd?id=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id =  session('u_openid');
        $user_id = session('u_openid');
        extract(input());
        $up = new Qiniu();
        $arr = '';
        if(isset($photo)){
            foreach($photo as $key=>$vo){
                $img = base64_img1 ($vo)['msg'];
                //取出图片后缀
                $type = explode(".",$img);
                $type = end($type);
                //组装图片名
                $key = md5(time().uuid()).'.'.$type;
                $arr[] = $up->uploadFile($img,$key,'http://qiniu.verynurse.com/');
                unlink($img);
            }
            $arr = implode (',',$arr).',';
        }
        $data['answer_user_id'] = $user_id;
        $data['answer_content'] = $content;
        $data['activities_id'] = $id;
        $data['answer_img'] = $arr;
        $data['answer_time'] = date('Y-m-d H:i:s', time());
        $res = Db::table('up_institutional_activities_comment')->insertGetId($data);
        if ($res) {
            $this->apiSuccess();
        } else {
            $this->apiError('K100');
        }
    }
    /***
     * 活动评论回复插入数据
     */
    public function Reply(){

        $user_id = session('u_openid');
        $answer_id = input('param.answer_id');
        $content  = input('param.content');
        $data['reply_user_id'] = $user_id;
        $data['reply_information_answer_id'] = $answer_id;
        $data['reply_content'] = $content;
        $data['reply_create_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_institutional_activities_answer')->insertGetId($data);
        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('E000');
        }
    }
    /***
     * @return mixed
     * 活动评论更多回复
     */
    public function moreanswer(){

//        $user_id  = session('u_openid');
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        session('target_url',"http://south.xuanwumobile.com/home/First/moreanswer?id=".$id.'&page='.$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $user_id = session('u_openid');
        $num = $page - 1;
        $res = Db::table('up_institutional_activities_comment')//评论
        ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
            ->where('answer_id',$id)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->find();
            $day = date('Y-m-d',time());//当前时间
            $time = time() - 3600*24;//昨天的时间戳
            $last_time = date('Y-m-d 00:00:00',$time);//昨天开始
            $last_time1 = date('Y-m-d 23:59:59',$time);//昨天结束
            if($day==date('Y-m-d',strtotime($res['answer_time']))){//判断是否是今天
                $res['answer_time'] ='今天'.' '.date('H:i',strtotime($res['answer_time']));
            }elseif ($last_time<$res['answer_time']&&$res['answer_time']<$last_time1)
           {//判断是否是昨天
               $res['answer_time'] ='昨天'.' '.date('m-d H:i',strtotime($res['answer_time']));
           }
        $where2['data_id'] = $res['answer_id'];
        $where2['type'] = 1;
        $where2['user_id'] = $user_id;
        $dz_ids = Db::table('up_dz')->where($where2)->value('dz_id');
        if($dz_ids){//证明点赞过了
            $res['dz'] = 1;
        }else{//没有点赞
            $res['dz'] = 2;
        }
        if($res['answer_user_id']==$user_id){
            $res['is_delete'] = 1;//证明是自己发布的
        }else{
            $res['is_delete'] = 2;
        }
        $aa = explode(',',trim($res['answer_img'],','));
        if(count(array_filter($aa))>0){
            $res['answer_img'] = $aa;
        }else{
            $res['answer_img'] = array();
        }
        $count= Db::table('up_institutional_activities_answer')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->where('a.reply_information_answer_id',$res['answer_id'])
            ->count();
        $res['count'] = $count;
        $a= Db::table('up_institutional_activities_answer')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->where('a.reply_information_answer_id',$res['answer_id'])
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->order('a.reply_create_time desc')
            ->limit($num*10,10)
            ->select();
        foreach($a as $key=>&$vo){

            $where['type'] = 2;
            $where['user_id'] = $user_id;
            $where['data_id'] = $vo['reply_id'];
            $dz_id = Db::table('up_dz')->where($where)->value('dz_id');
            if($dz_id){//证明点赞过了
                $vo['dz'] = 1;
            }else{//没有点赞
                $vo['dz'] = 2;
            }
//            if($user_id == $vo['reply_user_id']){//证明是自己发布
//                $vo['my_delete'] = 1;
//            }else{//不是自己发布的
//                $vo['my_delete'] = 2;
//            }
            if($day==date('Y-m-d',strtotime($vo['reply_create_time']))){//判断是否是今天
                $vo['reply_create_time'] ='今天'.' '.date('H:i',strtotime($vo['reply_create_time']));
            }elseif ($last_time<$vo['reply_create_time']&&$vo['reply_create_time']<$last_time1)
            {//判断是否是昨天
                $vo['reply_create_time'] ='昨天'.' '.date('m-d H:i',strtotime($vo['reply_create_time']));
            }
        }
        $res['more'] = $a;
        $this->assign('res',$res);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /***
     *活动评论回复加载更多
     */
    public function ajaxmoreanswer(){
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        session('target_url',"http://south.xuanwumobile.com/home/First/ajaxmoreanswer?id=".$id.'&page='.$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id = session('u_openid');

        $num = $page - 1;
        $user_id = session('u_openid');
        $day = date('Y-m-d',time());//当前时间
        $time = time() - 3600*24;//昨天的时间戳
        $last_time = date('Y-m-d 00:00:00',$time);//昨天开始
        $last_time1 = date('Y-m-d 23:59:59',$time);//昨天结束
        $a= Db::table('up_institutional_activities_answer')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->where('a.reply_information_answer_id',$id)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->order('a.reply_create_time desc')
            ->limit($num*10,10)
            ->select();
        foreach($a as $key=>&$vo){

            $where['type'] = 2;
            $where['user_id'] = $user_id;
            $where['data_id'] = $vo['reply_id'];
            $dz_id = Db::table('up_dz')->where($where)->value('dz_id');
            if($dz_id){//证明点赞过了
                $vo['dz'] = 1;
            }else{//没有点赞
                $vo['dz'] = 2;
            }
//            if($user_id == $vo['reply_user_id']){//证明是自己发布
//                $vo['my_delete'] = 1;
//            }else{//不是自己发布的
//                $vo['my_delete'] = 2;
//            }
            if($day==date('Y-m-d',strtotime($vo['reply_create_time']))){//判断是否是今天
                $vo['reply_create_time'] ='今天'.' '.date('H:i',strtotime($vo['reply_create_time']));
            }elseif ($last_time<$vo['reply_create_time']&&$vo['reply_create_time']<$last_time1)
            {//判断是否是昨天
                $vo['reply_create_time'] ='昨天'.' '.date('m-d H:i',strtotime($vo['reply_create_time']));
            }
        }
        return json(['status'=>200,'msg'=>$a]);
    }

    /***
     * 活动评论删除
     */
    public function delete(){
        $user_id = session('u_openid');
        $answer_id = input('param.answer_id');
        $type = input('param.type');
        Db::startTrans();
        if($type==1){//评论删除
            $answer_user_id = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->value('answer_user_id');
            if($answer_user_id!=$user_id){
                $this->apiError('E134');
            }
            $res = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->delete();
            $reply_id =Db::table('up_institutional_activities_answer')->where('reply_information_answer_id',$answer_id)->value('reply_id');
            if(!$reply_id){
                Db::table('up_institutional_activities_answer')->where('reply_information_answer_id',$answer_id)->delete();
            }
            $where1['data_id'] = $answer_id;
            $where1['type'] = 1;
            $dz_id =Db::table('up_dz')->where($where1)->value('dz_id');
            if(!$dz_id){
//            $where1['user_id'] = $user_id;
                Db::table('up_dz')->where($where1)->delete();
            }
            if($res){
                Db::commit();
                $this->apiSuccess();
            }else{
                Db::rollback();
                $this->apiError('E001');
            }
        }elseif ($type==2){//评论回复删除
            //查询评论的发布人
            $reply_user_id = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->value('reply_user_id');
            if($reply_user_id!=$user_id){//如果不是同一个用户不能删除
                $this->apiError('E134');
            }
            $res = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->delete();
            $where1['data_id'] = $answer_id;
            $where1['type'] = 2;
            $dz_id =Db::table('up_dz')->where($where1)->value('dz_id');
            if(!$dz_id){
//                $where1['user_id'] = $user_id;
                Db::table('up_dz')->where($where1)->delete();
            }
            if($res){
                Db::commit();
                $this->apiSuccess();
            }else{
                Db::rollback();
                $this->apiError('E001');
            }
        }else{
            $this->apiError('E908');
        }
    }
    //活动评论，活动评论回复点赞 取消点赞
    public function dzactivity(){
        $user_id = session('u_openid');
        $answer_id = input('param.answer_id');
        $type = input('param.type');
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $where['data_id'] = $answer_id;
        $dz_id = Db::table('up_dz')->where($where)->value('dz_id');//查询这个用户是否已经点赞
        if($type==1){//评论点赞
            if($dz_id){//已经点赞就取消点赞
                $res = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->setDec('answer_dz',1);
                $list = Db::table('up_dz')->where($where)->delete();
            }else{//点赞插入数据
                $res = Db::table('up_institutional_activities_comment')->where('answer_id',$answer_id)->setInc('answer_dz',1);
                $data['user_id'] = $user_id;
                $data['type'] = 1;
                $data['data_id'] = $answer_id;
                $list = Db::table('up_dz')->insertGetId($data);
            }
            if($list&&$res){
                $this->apiSuccess();
            }else{
                $this->apiError('E133');
            }
        }elseif ($type==2){//评论回复点赞
            if($dz_id){//已经点赞就取消点赞
                $res = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->setDec('reply_thumbs_up',1);
                $list = Db::table('up_dz')->where($where)->delete();
            }else{//点赞插入数据
                $res = Db::table('up_institutional_activities_answer')->where('reply_id',$answer_id)->setInc('reply_thumbs_up',1);
                $data['user_id'] = $user_id;
                $data['type'] = 2;
                $data['data_id'] = $answer_id;
                $list = Db::table('up_dz')->insertGetId($data);
            }
            if($list&&$res){
                $this->apiSuccess();
            }else{
                $this->apiError('E133');
            }
        }
    }
    //收藏，取消收藏
    public function sc(){
        //todo 缺少用户id
        $user_id = session('u_openid');
        $id = input('param.id');
        $where['user_id'] = $user_id;
        $where['institutional_activities_id'] = $id;
        $my_activities = Db::table('up_my_activities')->where($where)->value('id');
        if($my_activities){
            $res = Db::table('up_my_activities')->where($where)->delete();
            $collect = 2;//是否收藏  1已收藏  2未收藏
            if($res){
                $this->apiSuccess(['collect'=>$collect]);
            }else{
                $this->apiError('E001');
            }
        }else{
            $data['user_id'] = $user_id;
            $data['institutional_activities_id'] = $id;
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_my_activities')->insertGetId($data);
            $collect = 1;//是否收藏  1已收藏  2未收藏
            if($res){
                $this->apiSuccess(['collect'=>$collect]);
            }else{
                $this->apiError('E001');
            }
        }

    }
    /***
     * @return mixed
     * 报名
     */
    public function bm(){
        $id = input('param.activity');

        session('target_url',"http://south.xuanwumobile.com/home/First/bm?activity=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id = session('u_openid');
        $user_id = session('u_openid');
        $userModel  = new UserModel();
        $count = Db::table('up_institutional_activities_member')->where('institutional_activities_id',$id)->count();
        $list = Db::table('up_institutional_activities')->where('id',$id)->field('enrolment,rang_enrolment')->find();
        if($list['enrolment']==2){
            if($count>=$list['rang_enrolment']){
                $this->apiError('E137');
            }
        }
        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_to_mechanism_id']==-1){
            $this->apiError('E135');
        }

        if($res['user_attestation']==3){
            //查询职务
            $assortment_name =Db::table('up_assortment')->where('assortment_id',$res['user_post'])->value('assortment_name');
            $mechanism_name =Db::table('up_mechanism')->where('mechanism_id',$res['user_to_mechanism_id'])->value('mechanism_name');
            $res['assortment_name'] = $assortment_name;
            $res['mechanism_name'] = $mechanism_name;
        }
        $this->assign('res',$res);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /***
     * 提交报名判断
     */
    public function bmpd(){
        $user_id = session('u_openid');
        $userModel  = new UserModel();
        $id = input('param.activity');
        $count = Db::table('up_institutional_activities_member')->where('institutional_activities_id',$id)->count();
        $list = Db::table('up_institutional_activities')->where('id',$id)->field('enrolment,rang_enrolment')->find();
        if($list['enrolment']==2){
            if($count>=$list['rang_enrolment']){
                $this->apiError('E137');
            }
        }
        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_attestation']==-1){
            $this->apiError('E135');
        }
        if($res['user_attestation']!=3){
            $this->apiError('E138');
        }
        $this->apiSuccess();
    }
    /**
     * 提交报名判断
     */
    public function postSign(){
        $userModel  = new UserModel();
        $user_id = session('u_openid');
        $now_time = time();//报名的现在时间
        $id = input('param.id');//活动id
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();

        $field = 'user_id,user_username,user_phone,user_attestation,user_post,user_to_mechanism_id,user_to_mechanism_id';
        $res = $userModel->selectUserInfo($user_id,$field);
        if($res['user_to_mechanism_id']==-1){//没有加入机构的用户不能报名
            $this->apiError('E135');
        }
        if($res['user_attestation']!=3){//没有认证成功的用户不能报名
            $this->apiError('E135');
        }
        $where['id'] = $id;
        $info = $InstitutionalActivitiesModel->getInfoList($where);//获取单条活动信息
        if(strtotime($info['activity_sign_start'])>0&&strtotime($info['activity_sign_end'])>0){//设置活动报名的时间
            if(strtotime($info['activity_sign_start'])<$now_time
                &&$now_time <strtotime($info['activity_sign_end'])){//时间段之内
                $this->thisMethod($user_id,$id,$info['activity_name'],$info['activity_mechanism_id']);
            }else{
                $this->apiError('K101');
            }
        }else{//没有设置活动报名时间
            if(strtotime($info['create_time'])<$now_time
                &&$now_time <strtotime($info['activity_end_time'])){
                $this->thisMethod($user_id,$id,$info['activity_name'],$info['activity_mechanism_id']);
            }else{
                $this->apiError('K101');
            }
        }
    }

    /***
     * @param $user_id
     * @param $id
     * 报名
     */
    protected function thisMethod($user_id,$id,$activity_name,$activity_mechanism_id){
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        $data['user_id'] = $user_id;
        $data['institutional_activities_id'] = $id;
        $member_id = Db::table('up_institutional_activities_member')->where($data)->value('id');//查询该用户是否已经报名
        if($member_id){
            $this->apiError('E122');
        }else{
            $res = Db::table('up_institutional_activities_member')->insertGetId($data);//插入活动报名表
            $InstitutionalActivitiesModel->where('id',$id)->setInc('activity_num',1);//报名人数每次加1
            if($res){
                $whereMap['n_type'] = 4;
                $whereMap['n_activitie'] = $id;
                $resList = Db::table('up_notice')->where($whereMap)->find();//查询这个消息是否已经添加
                if(!$resList['n_id']){//证明没有添加消息
                    $datas['n_createtime'] =date('Y-m-d H:i:s',time());
                    $datas['n_title'] ="您报名的".$activity_name."报名成功";
                    $datas['n_read_user'] =',0,';
                    $datas['n_clear_user'] =',0,';
                    $datas['n_user_id'] =',0,'.$user_id.',';
                    $datas['n_activitie'] =$id;
                    $datas['n_mechanism'] =$activity_mechanism_id;
                    $datas['n_type'] =4;
                    //插入消息表里面
                    Db::table('up_notice')->insert($datas);
                    //todo  缺少极光推送
                }else{//添加了
                    $uid = ','.$user_id.',';
                    if(strpos($resList['n_user_id'],$uid) !==false){

                    }else{
                        $str = $resList['n_user_id'].$uid.',';
                        //更新发送
                        Db::table('up_notice')->where('n_id',$resList['n_id'])->setField('n_user_id',$str);
                    }
                    //todo 缺少极光推送
                }
                $this->apiSuccess();
            }else{
                $this->apiError('K100');
            }
        }
    }

    /***
     * 取消报名
     */
    public function cancelSign(){
        $user_id = session('u_openid');
        $id = input('param.id');//活动ID

        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        $where1['user_id'] = $user_id;
        $where1['institutional_activities_id'] = $id;
        //查询这个用户的报名这个活动
        $sign_status_info = Db::table('up_institutional_activities_member')->where($where1)->find();
        if(!$sign_status_info['id']){//证明没有报名，不能取消报名
            $this->apiError('E123');
        }
//        else{
//            if($sign_status_info['sign_status']==2){//已经签到不能取消报名
//                $this->apiError('E129');
//            }
//        }
        $now_time = time();//取消报名的现在时间
        $where['id'] = $id;
        $info = $InstitutionalActivitiesModel->getInfoList($where);//获取单条活动信息
        if($info['activity_status']==3){
            $this->apiError('E124');
        }
        if(strtotime($info['activity_sign_start'])>0&&strtotime($info['activity_sign_end'])>0){
            //设置活动报名的时间
            if(strtotime($info['activity_sign_start'])<$now_time
                &&$now_time <strtotime($info['activity_sign_end']))
            {//时间段之内
                $this->thisSign($user_id,$id);//取消报名

            }else{
//                if(strtotime($info['activity_sign_end'])<=strtotime($info['activity_start_time'])){//活动报名时间和活动开始时间相等
//                    $time = strtotime($info['activity_start_time'])-7200;//前2个小时
//                    if($time<$now_time&&$now_time<strtotime($info['activity_start_time'])){//在这个时间段里面不可以取消报名
                //不可以取消报名
                $this->apiError('E124');
//                    }else{
//                        //取消报名
//                        $this->thisSign($user_id,$id);
//                    }
//                }
            }
        }else{//没有设置活动报名时间
            if(strtotime($info['create_time'])<$now_time &&$now_time <strtotime($info['activity_end_time'])){
                //可以取消报名
                $this->thisSign($user_id,$id);
            }else{//不可以取消取消报名
                $this->apiError('E124');
            }
        }
    }

    /**
     * @param $user_id
     * @param $id
     * 取消报名
     */
    protected function thisSign($user_id,$id){
        $InstitutionalActivitiesModel=new InstitutionalActivitiesModel();
        $data['user_id'] = $user_id;
        $data['institutional_activities_id'] = $id;
        $member_id = Db::table('up_institutional_activities_member')->where($data)->value('id');//查询该用户是否已经报名
        if(!$member_id){
            $this->apiError('E123');
        }else{
            $res = $InstitutionalActivitiesModel->where('id',$id)->setDec('activity_num',1);//报名人数每次减1
            Db::table('up_institutional_activities_member')->where($data)->delete();//删除该用户报名

            if($res){
                $this->apiSuccess();
            }else{
                $this->apiError('K100');
            }
        }
    }
    /***
     * 签到页面
     */
    public function sign(){
        $id = input('param.id');

        session('target_url',"http://south.xuanwumobile.com/home/First/sign?id=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id = session('u_openid');
        $options = Config::get('wechat');
        $app = new Application($options);
        $js = $app->js;
        $json = $js->config(array('getLocation'), false,false,true);
        $this->assign('json',$json);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 签到请求数据
     */
    public function signActibity(){
//        $user_id =session('u_openid');
        $id = input('param.id');//活动ID

        session('target_url',"http://south.xuanwumobile.com/home/First/signActibity?id=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $user_id = session('u_openid');

        $where['user_id'] = $user_id;
        $where1['a.id'] = $id;
        //这条活动的信息
        $list = Db::table('up_institutional_activities')->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.activity_mechanism_id','left')
            ->where($where1)->field('a.*,um.mechanism_name')->find();
        $activity_name =Db::table('up_activity')->where('id','IN',trim($list['activity_id'],','))->column('activity_name');
        $list['name'] = implode(',',$activity_name);
        $where['institutional_activities_id'] = $id;
//        $my_activities = Db::table('up_my_activities')->where($where)->value('id');//查询该用户该活动是否已经收藏

        //查询该用户该活动是否已经参加这个活动
        $my_activities_member = Db::table('up_institutional_activities_member')->where($where)->find();
        $now_time = time();//报名的现在时间
        if(strtotime($list['activity_sign_start'])>0&&strtotime($list['activity_sign_end'])>0){
            //设置活动报名的时间
//            if(strtotime($list['activity_sign_start'])<$now_time &&
//                $now_time <strtotime($list['activity_sign_end'])){
//                if($my_activities_member['sign_status']==1){
//                    $whereMap['user_id'] = $user_id;
//                    $whereMap['institutional_activities_id'] = $id;
//                    Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
//                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
//                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                }else{
//                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
//                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                }
//            }else{
//                if(strtotime($list['activity_sign_end'])<$now_time&&
//                    $now_time<strtotime($list['activity_start_time'])-7200){
//                    $list['already_sign'] = 5;//活动的报名结束时间到活动的开始时间的空白期
//                }else{
//                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
//                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                }
//            }
            //设置活动报名的时间
            if(strtotime($list['activity_sign_start'])<$now_time && $now_time <strtotime($list['activity_sign_end'])-7200){

                $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                $list['already_sign'] = $my_activities_member2['sign_status'];
            }else{
                if(strtotime($list['activity_sign_end'])<$now_time&&
                    $now_time<strtotime($list['activity_start_time'])-7200){
                    $list['already_sign'] = 5;//活动的报名结束时间到活动的开始时间的空白期
                }else{
                    if(strtotime($list['activity_start_time'])-7200<$now_time&&$now_time<strtotime($list['activity_end_time'])){
//                        $whereMap['user_id'] = $user_id;
//                        $whereMap['institutional_activities_id'] = $id;
//                        Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                        if($my_activities_member['sign_status'] ==1){
                            $whereMap['user_id'] = $user_id;
                            $whereMap['institutional_activities_id'] = $id;
                            Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                        }
                        $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                        $list['already_sign'] = $my_activities_member2['sign_status'];
                    }else{
                        $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                        $list['already_sign'] = $my_activities_member2['sign_status'];
                    }

                }
            }
        }else{//没有设置活动报名时间
            if(strtotime($list['create_time'])<$now_time&&$now_time <strtotime($list['activity_end_time'])){
                if($my_activities_member['sign_status']==1){
                    $whereMap['user_id'] = $user_id;
                    $whereMap['institutional_activities_id'] = $id;
                    Db::table('up_institutional_activities_member')->where($whereMap)->setField('sign_status',2);
                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                    $list['already_sign'] = $my_activities_member2['sign_status'];
                }else{
                    $my_activities_member2 = Db::table('up_institutional_activities_member')->where($where)->find();
                    $list['already_sign'] = $my_activities_member2['sign_status'];
//                    $list['already_sign'] = $my_activities_member['sign_status'];
                }
            }
        }
        if($my_activities_member['id']==''){//该用户该活动的么有报名状态
            $list['already_sign'] = 4;
            $list['sign_time_member'] ='0000-00-00 00:00:00';
            $list['sign_end_time_member'] ='';
        }else{
            $list['sign_time_member'] =$my_activities_member['sign_time'];
            $list['sign_end_time_member'] =$my_activities_member['sign_end_time'];
        }
        return json(['status'=>200,'data'=>$list]);
    }

    /***
     * 签到
     */
    public function SignIn(){
        $user_id = session('u_openid');
        $id = input('param.id');//活动ID
        $now_time = time();//签到时间
        $where['institutional_activities_id'] = $id;//活动ID
        $where['user_id'] = $user_id;//用户ID
        $activity_start_time = Db::table('up_institutional_activities')//查询这个活动的开始时间
        ->where('id',$id)
        ->value('activity_start_time');
        $sign_time = strtotime($activity_start_time) - 7200;//签到的前2个小时
        if($sign_time<$now_time&&$sign_time<strtotime($activity_start_time)){//在这个时间段才可以签到

            $res = Db::table('up_institutional_activities_member')
                ->where($where)
                ->find();
            if(!$res['id']){
                $this->apiError('E123');
            }else{
//                if($res['sign_status']==2){//证明已经签到中
                //签到更新数据库字段
                $data['sign_time'] = date('Y-m-d H:i:s',time());
                $data['sign_status'] = 3;
                $data['sign_longitude'] =input('param.sign_longitude');
                $data['sign_latitude'] =input('param.sign_latitude');
                $whereMap['user_id'] = $user_id;
                $whereMap['institutional_activities_id'] = $id;
                $list = Db::table('up_institutional_activities_member')->where($whereMap)->update($data);

                if($list){
                    return json(['status'=>200,'data'=>$id]);
                }else{
                    $this->apiError('E127');
                }
//                }
            }
        }else{
            $this->apiError('E128');
        }
    }

    /***
     * 签退
     */
    public function SignBack(){
        $user_id = session('u_openid');
        $id = input('param.id');//活动ID
        $now_time = time();//签退时间
        $where['user_id'] = $user_id;
        $where['institutional_activities_id'] = $id;
        $res = Db::table('up_institutional_activities_member')->where($where)->find();
        if(!$res['id']){
            $this->apiError('E123');
        }else{
            $activity_end_time= Db::table('up_institutional_activities')->where('id',$id)->field('activity_end_time')->find();
//                if($res['sign_status']==3){
        if($now_time>strtotime($activity_end_time['activity_end_time'])){
            $this->apiError('E130');
        }else{
            $data['sign_status']  =3;
            $data['sign_end_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_institutional_activities_member')->where($where)->update($data);
            return json(['status'=>200,'data'=>$id]);

           }
//            }
        }
    }
    /***
     * @return mixed
     * 志愿者列表
     */
    public function volunteer(){
        $page = input('param.page')?input('param.page'):1;
        session('target_url',"http://south.xuanwumobile.com/home/First/volunteer?page=".$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id = session('u_openid');
        $num = $page-1;
        $pageSize = 10;
        $list = Db::table('up_volunteer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('uuu.user_username,uuu.user_image,uuu.user_to_mechanism_id,a.*')
            ->limit($num*$pageSize,$pageSize)
            ->order('a.volunteer_sort desc')
            ->select();
        foreach ($list as $key=>&$vo){
            $res = Db::table('up_institutional_activities_member')->alias('b')
                ->join('up_institutional_activities uia','uia.id = b.institutional_activities_id','left')
                ->where('b.user_id',$vo['user_id'])
                ->column('activity_name');
            $vo['activity'] = implode(',',$res);
            //查询自己属于哪个机构的
            $mechanism = Db::table('up_mechanism')->where('mechanism_id',$vo['user_to_mechanism_id'])->value('mechanism_name');
            $vo['mechanism'] = $mechanism;
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 志愿者ajax请求
     */
    public function ajaxvolunteer(){
        $page = input('param.page')?input('param.page'):1;
        $num = $page-1;
        $pageSize = 10;
        $list = Db::table('up_volunteer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('uuu.user_username,uuu.user_image,uuu.user_to_mechanism_id,a.*')
            ->limit($num*$pageSize,$pageSize)
            ->order('a.volunteer_sort desc')
            ->select();
        foreach ($list as $key=>&$vo){
            $res = Db::table('up_institutional_activities_member')->alias('b')
                ->join('up_institutional_activities uia','uia.id = b.institutional_activities_id','left')
                ->where('b.user_id',$vo['user_id'])
                ->column('activity_name');
            $vo['activity'] = implode(',',$res);
            //查询自己属于哪个机构的
            $mechanism = Db::table('up_mechanism')->where('mechanism_id',$vo['user_to_mechanism_id'])->value('mechanism_name');
            $vo['mechanism'] = $mechanism;
        }
        return json(['status'=>200,'msg'=>$list]);

    }
    /**
     * @return mixed
     * 志愿者详情
     */
    public function volunteerdetail(){
        $userModel = new  UserModel();
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $list = array();
        $user_id = input('param.user_id');
        $page = input('param.page')?input('param.page'):1;
        session('target_url',"http://south.xuanwumobile.com/home/First/volunteerdetail?page=".$page.'&user_id='.$user_id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
//        $user_id = session('u_openid');

        //查询自己参加的哪些活动
        $join_activities = Db::table('up_institutional_activities_member')->where('user_id',$user_id)->column('institutional_activities_id');
        $whereMap['a.id'] = array('IN',$join_activities);
        $join_list = $InstitutionalActivitiesModel->selectList($whereMap,'desc',$page);
        //用户信息
        $userList = $userModel->selectUserInfo($user_id);
        //查询用户基本信息
        $user_info = Db::table('up_u_user')->alias('a')
            ->join('up_area ua','ua.district_id=a.user_sheng','left')
            ->join('up_area ua1','ua1.district_id=a.user_shi','left')
            ->join('up_area ua2','ua2.district_id=a.user_qu','left')
            ->join('up_mechanism um','um.mechanism_id=a.user_to_mechanism_id','left')
            ->field('a.user_rel_name,um.mechanism_name,ua.district as sheng,ua1.district as shi
                                ,ua2.district as qu')
            ->where('a.user_id',$user_id)
            ->find();
        //获得的荣誉
        $welfareInfo = Db::table('up_volunteer')->where('user_id',$user_id)->value('content');
        $where['sign_time'] =array('>',0);
        $where['sign_end_time'] =array('>',0);
        $where['user_id'] =$user_id;

        //参加的活动
        $activities_member = Db::table('up_institutional_activities_member')
            ->alias('a')
            ->join('up_institutional_activities uia','uia.id = a.institutional_activities_id')
            ->where($where)
            ->field('uia.activity_time,a.*')
            ->select();
        $total_time = '';
        foreach ($activities_member as $key=>$vo){
            if(strtotime($vo['sign_end_time'])>0){
                $time = strtotime($vo['sign_end_time']) - strtotime($vo['sign_time']);
                if(floor($time/3600) > $vo['activity_time']){//如果签到的时长大于活动的举办时长
                    $total_time += $vo['activity_time'];
                }else{
                    $total_time += floor($time/3600);
                }
            }

        }
        $num = count($activities_member);//参加的活动次数
        $list['welfare'] = $welfareInfo;//荣誉
        $list['hospital'] = $this->getUserType(1,$userList['user_hospital']);//执业医院
        $list['department'] = $this->getUserType(2,$userList['user_department']);//科室
        $list['title'] = $this->getUserType(3,$userList['user_title']);//职称
        $list['post'] = $this->getUserType(4,$userList['user_post']);//职务
        $list['num'] = $num;//参加活动的次数
        $list['time'] = $total_time;//参加活动的时间
        $list['user'] = $user_info;//基本资料
        $img = explode(',',$userList['user_business_image']);
        $list['join'] = $join_list;
        foreach ($list['join'] as $key=>&$vv){
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        $this->assign('list',$list);
        $this->assign('img',$img);
        return $this->fetch();
    }

    /***
     * 志愿者活动ajax请求
     */
    public function ajaxvolunteerdetail(){
        $InstitutionalActivitiesModel = new InstitutionalActivitiesModel();
        $user_id = session('u_openid');
        $page = input('param.page')?input('param.page'):1;

        $join_activities = Db::table('up_institutional_activities_member')->where('user_id',$user_id)->column('institutional_activities_id');
        $whereMap['a.id'] = array('IN',$join_activities);
        $join_list = $InstitutionalActivitiesModel->selectList($whereMap,'desc',$page);
        foreach ($join_list as $key=>&$vv){
            $start_time = date('Ymd',strtotime($vv['activity_start_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'   '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }elseif (date('Y',strtotime($vv['activity_start_time']))!=date('Y',time())){
                //不同年
                $vv['activity_start_time'] = date('Y-m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('H:i',strtotime($vv['activity_end_time']));
            }else{
                $vv['activity_start_time'] = date('m-d',strtotime($vv['activity_start_time'])).'  '.date('H:i',strtotime($vv['activity_start_time']));
                $vv['activity_end_time'] =   date('m-d H:i',strtotime($vv['activity_end_time']));
            }
        }
        return json(['status'=>200,'msg'=>$join_list]);

    }
    /***
     * @param $type
     * @param $mold
     * @return mixed
     * 查询自己的信息
     */
    protected function getUserType($type,$mold){
        $where['assortment_type'] = $type;
        $where['assortment_id'] = $mold;
        $assortment_name = Db::table('up_assortment')->where($where)->value('assortment_name');
        return $assortment_name;
    }
    /***
     * @return mixed
     * 问答
     */
    public function answer(){

//        $user_id =  session('u_openid');
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type')?input('param.type'):1;
        session('target_url',"http://south.xuanwumobile.com/home/First/answer?page=".$page.'&type='.$type);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $num = $page-1;
        if($type==1){
            $now_time = date('Y-m-d H:i:s',time());
            $last_time = date('Y-m-d H:i:s',strtotime('-1 week'));
            $where['a.interlocution_time'] =array('between',array($last_time,$now_time));
            $order = 'interlocution_num desc';
        }elseif ($type==2){
            $order = 'interlocution_time desc';
            $where = 1;
        }
        $list = Db::table('up_interlocution')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->order($order)
            ->limit($num*10,10)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->select();
        foreach ($list as $key=>&$vv){
            $vv['interlocution_img'] = trim($vv['interlocution_img'],',');

            $start_time = date('Ymd',strtotime($vv['interlocution_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['interlocution_time'] = date('H:i',strtotime($vv['interlocution_time']));
            }elseif (date('Y',strtotime($vv['interlocution_time']))!=date('Y',time())){
                //不同年
                $vv['interlocution_time'] = date('Y-m-d',strtotime($vv['interlocution_time']));
            }else{
                $vv['interlocution_time'] = date('m-d',strtotime($vv['interlocution_time']));
            }
            $vv['img'] = explode(',',$vv['interlocution_img']);
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 回答ajax请求
     */
    public function ajaxanswerhot(){
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type')?input('param.type'):1;
        $num = $page-1;
        if($type==1){
            $now_time = date('Y-m-d H:i:s',time());
            $last_time = date('Y-m-d H:i:s',strtotime('-1 week'));
            $where['a.interlocution_time'] =array('between',array($last_time,$now_time));
            $order = 'interlocution_num desc';
        }elseif ($type==2){
            $order = 'interlocution_time desc';
            $where = 1;
        }
        $list = Db::table('up_interlocution')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->order($order)
            ->limit($num*10,10)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->select();
        foreach ($list as $key=>&$vv){
            $start_time = date('Ymd',strtotime($vv['interlocution_time']));
            if($start_time==date('Ymd',time())){//同年同月同日
                $vv['interlocution_time'] = date('m-d',strtotime($vv['interlocution_time']));
            }elseif (date('Y',strtotime($vv['interlocution_time']))!=date('Y',time())){
                //不同年
                $vv['interlocution_time'] = date('Y-m-d',strtotime($vv['interlocution_time']));
            }else{
                $vv['interlocution_time'] = date('m-d',strtotime($vv['interlocution_time']));
            }
            $vv['img'] = explode(',',$vv['interlocution_img']);
        }
        return json(['status'=>200,'msg'=>$list]);

    }


    /**
     * @return mixed
     * 问答详情
     */
    public function answerdetail(){
        $page = input('param.page')?input('param.page'):1;
        $id = input('param.id');
        session('target_url',"http://south.xuanwumobile.com/home/First/answerdetail?page=".$page.'&page='.$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);

        $num = $page-1;
        $user_id =session('u_openid');

        $list = Db::table('up_interlocution')->alias('ui')
            ->join('up_u_user uuu','uuu.user_id = ui.user_id','left')
            ->where('interlocution_id',$id)->field('ui.*,uuu.user_username')->find();
        if($list['user_id']==$user_id){
             $list['is_delete'] = 1;
        }else{
            $list['is_delete'] = 2;
        }
        $start_time = date('Ymd',strtotime($list['interlocution_time']));
        if($start_time==date('Ymd',time())){//同年同月同日
            $list['interlocution_time'] = date('H:i',strtotime($list['interlocution_time']));
        }elseif (date('Y',strtotime($list['interlocution_time']))!=date('Y',time())){
            //不同年
            $list['interlocution_time'] = date('Y-m-d',strtotime($list['interlocution_time']));
        }else{
            $list['interlocution_time'] = date('m-d',strtotime($list['interlocution_time']));
        }
        $list['interlocution_img'] = trim($list['interlocution_img'],',');

        $list['img'] = explode(',',$list['interlocution_img']);
        $res = Db::table('up_interlocution_answer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where('f_interlocution_id',$id)
            ->limit($num*10,10)
            ->field('a.*,uuu.user_username as username,uuu.user_image')
            ->order('a.answer_time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            $day = date('Y-m-d',time());//当前时间
            $time = time() - 3600*24;//昨天的时间戳
            $last_time = date('Y-m-d 00:00:00',$time);//昨天开始
            $last_time1 = date('Y-m-d 23:59:59',$time);//昨天结束

            if($day==date('Y-m-d',strtotime($vo['answer_time']))){//判断是否是今天
                $vo['answer_time'] =date('H:i',strtotime($vo['answer_time']));
            }elseif ($last_time<$vo['answer_time']&&$vo['answer_time']<$last_time1){
                //判断是否是昨天
                $vo['answer_time'] =date('m-d',strtotime($vo['answer_time']));
            }else{
                $vo['answer_time'] =date('Y-m-d',strtotime($vo['answer_time']));
            }

            $vo['answer_img'] = explode(',',$vo['answer_img']);
            $where['type'] = 5;
            $where['data_id'] = $vo['answer_id'];
            $where['user_id'] = $user_id;
            $dz_id = Db::table('up_dz')->where($where)->value('dz_id');
            if($dz_id){//点赞
                $vo['dz'] = 1;
            }else{
                $vo['dz'] = 2;//没有点赞
            }
        }

        $list['children'] = $res;
        $this->assign('list',$list);
        $this->assign('uid',3);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /**
     * ajax问答评论加载更多
     */
    public function ajaxanswerdetail(){
        $user_id = session('u_openid');
        $page = input('param.page')?input('param.page'):1;
        $id = input('param.id');
        $num = $page-1;
        $res = Db::table('up_interlocution_answer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where('f_interlocution_id',$id)
            ->limit($num*10,10)
            ->field('a.*,uuu.user_username as username,uuu.user_image')
            ->order('a.answer_time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['user_id']==$user_id){
                $vo['delete'] = 1;//显示删除
            }else{
                $vo['delete'] = 2;//不显示删除

            }
            $day = date('Y-m-d',time());//当前时间
            $time = time() - 3600*24;//昨天的时间戳
            $last_time = date('Y-m-d 00:00:00',$time);//昨天开始
            $last_time1 = date('Y-m-d 23:59:59',$time);//昨天结束

            if($day==date('Y-m-d',strtotime($vo['answer_time']))){//判断是否是今天
                $vo['answer_time'] =date('H:i',strtotime($vo['answer_time']));
            }elseif ($last_time<$vo['answer_time']&&$vo['answer_time']<$last_time1){
                //判断是否是昨天
                $vo['answer_time'] =date('m-d',strtotime($vo['answer_time']));
            }else{
                $vo['answer_time'] =date('Y-m-d',strtotime($vo['answer_time']));
            }

            $vo['answer_img'] = explode(',',$vo['answer_img']);
            $where['type'] = 5;
            $where['data_id'] = $vo['answer_id'];
            $where['user_id'] = $user_id;
            $dz_id = Db::table('up_dz')->where($where)->value('dz_id');
            if($dz_id){//点赞
                $vo['dz'] = 1;
            }else{
                $vo['dz'] = 2;//没有点赞
            }
        }
        return json(['status'=>200,'msg'=>$res]);

    }

    /***
     * 问答详情点赞
     */
    public function dz(){
        $id = input('param.answer_id');
        $user_id = session('u_openid');
        $where['type'] = 5;
        $where['data_id'] = $id;
        $where['user_id'] = $user_id;
        $dz = Db::table('up_dz')->where($where)->value('dz_id');
        if($dz){//证明已经点赞就取消点赞
            $res = Db::table('up_interlocution_answer')->where('answer_id',$id)->setDec('answer_zang',1);
            $list = Db::table('up_dz')->where($where)->delete();
        }else{//点赞
            $res = Db::table('up_interlocution_answer')->where('answer_id',$id)->setInc('answer_zang',1);
            $data['user_id'] = $user_id;
            $data['type'] = 5;
            $data['data_id'] = $id;
            $list = Db::table('up_dz')->insertGetId($data);
        }
        if($list&&$res){
            $this->apiSuccess();
        }else{
            $this->apiError('E133');
        }
    }

    /***
     * 删除问答
     */
    public function deletaanswer(){
        $user_id = session('u_openid');
        $id = input('param.id');
        $res = Db::table('up_interlocution')->where('interlocution_id',$id)->find();
        if($res['user_id']!=$user_id){
            $this->apiError('E134');
        }else{
            $list = Db::table('up_interlocution')->where('interlocution_id',$id)->delete();
            $answer_id = Db::table('up_interlocution_answer')->where('f_interlocution_id',$id)->value('answer_id');
            if(!$answer_id){
                Db::table('up_interlocution_answer')->where('f_interlocution_id',$id)->delete();
            }
            if($list){
                $this->apiSuccess();
            }else{
                $this->apiError('E001');
            }
        }
    }
    /***
     * 删除回答回复评论
     */
    public function deletadz(){
        $id = input('param.answer_id');
        $user_id = session('u_openid');
        $res = Db::table('up_interlocution_answer')->where('answer_id',$id)->find();
        if($res['user_id']!=$user_id){
            $this->apiError('E134');
        }else{
            $list = Db::table('up_interlocution_answer')->where('answer_id',$id)->delete();
            Db::table('up_interlocution')->where('interlocution_id',$res['f_interlocution_id'])->setDec('interlocution_num',1);

            if($list){
                $this->apiSuccess();
            }else{
                $this->apiError('E001');
            }
        }
    }
    /***
     * @return mixed
     * 我来回答
     */
    public function question(){
        $id = input('param.id');
        session('target_url',"http://south.xuanwumobile.com/home/First/question?id=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);

        $res = Db::table('up_interlocution')->where('interlocution_id',$id)->find();
        //微信
        $options = Config::get('wechat');
        $app = new Application($options);
        $js = $app->js;
        $json = $js->config(array('getLocation','chooseImage','uploadImage','downloadImage','getLocalImgData'), false ,false,true);
        $this->assign(['json'=>$json,'uid'=>input('uid')]);
        $this->assign('res',$res);
        return $this->fetch();
    }

    /***
     * 问答编辑
     */
    public function answerupdate(){
        $id = input('param.answer_id');

        session('target_url',"http://south.xuanwumobile.com/home/First/answerupdate?answer_id=".$id);
        $openid = $this->getOpenid();
        $this->getUserId($openid);

        $res = Db::table('up_interlocution')->where('interlocution_id',$id)->find();
        $res['interlocution_img'] = explode(',',trim($res['interlocution_img'],','));
        $rrr = Db::table('up_interlocution')->where('interlocution_id',$id)->value('interlocution_img');
        $res['img'] = trim($rrr,',');
        $options = Config::get('wechat');
        $app = new Application($options);
        $js = $app->js;
        $json = $js->config(array('getLocation','chooseImage','uploadImage','downloadImage','getLocalImgData'), false ,false,true);
        $this->assign(['json'=>$json,'uid'=>input('uid')]);
        $this->assign('res',$res);
        return $this->fetch();
    }
    /*
   * 回答内容插入数据库
   */
    public function updateAnswer()
    {
        extract(input());
        $up = new Qiniu();
        $arr = '';
        if (isset($photo)) {
            foreach ($photo as $key => $vo) {
                $img = base64_img1($vo)['msg'];
                //取出图片后缀
                $type = explode(".", $img);
                $type = end($type);
                //组装图片名
                $key = md5(time() . uuid()) . '.' . $type;
                $arr[] = $up->uploadFile($img, $key, 'http://qiniu.verynurse.com/');
                unlink($img);
            }
            $arr = implode(',', $arr) . ',';
        }
        if($arr==''){
            $img = $hidden_img;
        }else{
            $img = $arr.$hidden_img.',';
        }
        $param = [
            'user_id' => session('u_openid'),
            'interlocution_title' => $title,
            'interlocution_introduce' => $content,
            'interlocution_img' => $img,
        ];
        Db::name('interlocution')->where('interlocution_id', $interlocution_id)->update($param);
        return json(['code' => 200, 'msg' => $arr, 'interlocution_id' => $interlocution_id]);
    }
    /*
     * 回答内容插入数据库
     */
    public function addAnswer(){
        extract(input());
        $up = new Qiniu();
        $arr = '';
        if(isset($photo)){
            foreach($photo as $key=>$vo){
                $img = base64_img1 ($vo)['msg'];
                //取出图片后缀
                $type = explode(".",$img);
                $type = end($type);
                //组装图片名
                $key = md5(time().uuid()).'.'.$type;
                $arr[] = $up->uploadFile($img,$key,'http://qiniu.verynurse.com/');
                unlink($img);
            }
            $arr = implode (',',$arr).',';
        }

        $param = [
            'user_id'=>session('u_openid'),
            'answer_time'=>date('Y-m-d H:i:s'),
            'answer_zang'=>'',
            'answer_content'=>$content,
            'answer_img'=>$arr,
            'f_interlocution_id'=>$interlocution_id
        ];
        $res = Db::name('interlocution_answer')->insert($param);
        if($res){
            Db::table('up_interlocution')->where('interlocution_id',$interlocution_id)->setInc('interlocution_num',1);

            return json(['code'=>200,'msg'=>$arr,'interlocution_id'=>$interlocution_id]);
        }else{
            return json(['code'=>100,'msg'=>'回答失败']);
        }
    }

    /**
     * @return mixed
     * 问答提问
     */
    public function wdanswer(){
        session('target_url',"http://south.xuanwumobile.com/home/First/wdanswer");
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $options = Config::get('wechat');
        $app = new Application($options);
        $js = $app->js;
        $json = $js->config(array('getLocation','chooseImage','uploadImage','downloadImage','getLocalImgData'), false ,false,true);
        $this->assign(['json'=>$json,'uid'=>input('uid')]);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 问答插入数据
     */
    public function addwd(){
        extract(input());
        $up = new Qiniu();
        $arr = '';
        if(isset($photo)){
            foreach($photo as $key=>$vo){
                $img = base64_img1 ($vo)['msg'];
                //取出图片后缀
                $type = explode(".",$img);
                $type = end($type);
                //组装图片名
                $key = md5(time().uuid()).'.'.$type;
                $arr[] = $up->uploadFile($img,$key,'http://qiniu.verynurse.com/');
//                $aaa= $up->uploadFile($img,$key,'http://qiniu.verynurse.com/');
                unlink($img);
            }
            $arr = implode (',',$arr).',';
        }
        if(!isset($title)){
           $title  = '';
        }
        if(!isset($content)){
            $content = '';
        }
//        session('u_openid')
        $param = [
            'user_id'=>session('u_openid'),
            'interlocution_time'=>date('Y-m-d H:i:s'),
            'interlocution_title'=>$title,
            'interlocution_introduce'=>$content,
            'interlocution_img'=>$arr,
            'interlocution_num'=>0
        ];
        $res = Db::name('interlocution')->insert($param);
        if($res){
            return json(['code'=>200,'msg'=>$arr]);
        }else{
            return json(['code'=>100,'msg'=>'回答失败']);
        }
    }

    /***
     * @return mixed
     * 消息页面
     */
    public function message(){
        $page = input('param.page')?input('param.page'):1;

        session('target_url',"http://south.xuanwumobile.com/home/First/message?page=".$page);
        $openid = $this->getOpenid();
        $this->getUserId($openid);
        $user_id =  session('u_openid');
        $uid = ','.$user_id.',';
        $where['n_user_id'] = array('like',"%$uid%");
        $where['n_clear_user'] = array('notlike',"%$uid%");
        $data  = Db::table('up_notice')->where($where)
            ->field('n_createtime,n_id,n_title,n_read_user,n_type,n_activitie,n_img,n_status')
            ->limit(($page-1)*10,10)->select();
        foreach ($data as $key=>&$vo){
            $whereMap['n_id'] = $vo['n_id'];
            $whereMap['n_read_user'] = array('like',"%$uid%");
            $n_id  = Db::table('up_notice')->where($whereMap)->value('n_id');
            if($n_id){
                $vo['read'] = 1;//已读
            }else{
                $vo['read'] = 2;//未读
            }
            if($vo['n_type']!=1){
                $activity_img = Db::table('up_institutional_activities')->where('id',$vo['n_activitie'])->value('activity_img');
                $num = count($activity_img);
                $list = explode(',',$activity_img);
                $vo['n_img'] =$list[$num-$num] ;
            }
        }
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     * 消息已读
     */
    public function readmessage(){
        $user_id = session('u_openid');
        $aid = input('param.n_id');
        $n_read_user = Db::table('up_notice')->where('n_id',$aid)->value('n_read_user');
        $uid = ','.$user_id.',';
        if(strpos($n_read_user,$uid) ===false){
            $str = $n_read_user.$user_id.',';
            //更新发送
            Db::table('up_notice')->where('n_id',$aid)->setField('n_read_user',$str);
            $this->apiSuccess();
        }else{
            return json(array('status'=>201));
        }
    }

    /***
     * 消息详情
     */
    public function messagedetail(){
        $n_id = input('param.n_id');
        $list   = Db::table('up_notice')->where('n_id',$n_id)->field('n_detail')->find();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /***
     * ajax加载数据
     */
    public function ajaxmessage(){
        $user_id = session('u_openid');
        $uid = ','.$user_id.',';
        $page = input('param.page')?input('param.page'):1;
        $where['n_user_id'] = array('like',"%$uid%");
        $where['n_clear_user'] = array('notlike',"%$uid%");
        $data  = Db::table('up_notice')->where($where)
            ->field('n_createtime,n_id,n_title,n_read_user,n_type,n_activitie,n_img,n_status')
            ->limit(($page-1)*10,10)->select();
        foreach ($data as $key=>&$vo){
            $whereMap['n_id'] = $vo['n_id'];
            $whereMap['n_read_user'] = array('like',"%$uid%");
            $n_id  = Db::table('up_notice')->where($whereMap)->value('n_id');
            if($n_id){
                $vo['read'] = 1;//已读
            }else{
                $vo['read'] = 2;//未读
            }
            if($vo['n_type']!=1){
                $activity_img = Db::table('up_institutional_activities')->where('id',$vo['n_activitie'])->value('activity_img');
                $num = count($activity_img);
                $list = explode(',',$activity_img);
                $vo['n_img'] =$list[$num-$num] ;
            }
        }
        return json(['code'=>100,'msg'=>$data]);

    }
}